let aaa="";
$(function () {
    $(".gxwltab").hide();
    $("#cpwlmx1").hide();
    $("#table11").hide();
    $("#tab22").hide();

    $("#gxwl").bootstrapTable({
        method: "post",
        contentType: 'application/json',
        url: "/gxwl/gxwlsjd",
        striped: true,
        pageNumber: 1,
        pagination: true,
        sidePagination: 'server',
        pageSize: 3,
        pageList: [2,3,5,6],
        queryParams: function (parmes) {
            return {
                pageNum: (parmes.offset / parmes.limit) + 1,
                pageSize: parmes.limit,
                registerTime:$("#registerTime").val(),
                checked:$("#checked").val(),
            }
        },
        columns: [
            {title: "工序设计单号", field: "designId", align: 'center'},
            {title: "产品编号", field: "productId", align: 'center'},
            {title: "产品名称", field: "productName", align: 'center'},
            {title: "设计人", field: "designer", align: 'center'},
            {title: "登记时间", field: "registerTime", align: 'center'},
            {title: "工时总成本", field: "costPriceSum", align: 'center'},
            {title: "制定", field: "id", align: 'center',
                formatter: function (value, row, index) {
                    let a="<a href='#' onclick='zhiding("+JSON.stringify(row)+")'>制定</a>";
                    return a;
                }
            },
        ]
    })
})

function cxks(){
    $("#gxwl").bootstrapTable("refresh");
}
let iid=0;
let aar=[];
function zhiding(row) {

    $.ajax({
        method: "post",
        contentType: 'application/json',
        url: "/DFile/jdcx/"+row.productId,
        dataType:"json",
        async:true,
        success:function (data) {
            iid=row.id;
            $(".gxwltab").show();

            $("#zhudiv").hide();
            $("#table11").hide();
            $("#tab22").hide();

            $("#gxdbh").html(row.designId);
            $("#sjr").html(row.designer);
            $("#name").html(row.productName);
            $("#id").html(row.productId);
            $("#zcb").html(row.costPriceSum);
            $("#yq").html(row.procedureDescribe);
            $("#shr").html(row.checker);
            $("#shtime").html(row.checkTime);

            $.ajax({
                method: "post",
                contentType: 'application/json',
                url: "/gxwl/gxmxwlsj/"+row.id,
                dataType:"json",
                async:true,
                success:function (data) {
                    let tab=document.getElementById("wlsjmx");
                    for (let i=0;i<data.length;i++){
                        aar[i]=data[i].id;
                        // aaa.push(data[i].procedureId)
                        let modelTotal = data[i].moduleSubtotal;
                        if (modelTotal==undefined){
                            modelTotal="";
                        }
                        let tag = data[i].designModuleTag;
                        if (tag=='0'){
                            tag="<a href='#' onclick='sheji("+data[i].id+",this)'>设计</a>";
                        }else if (tag=='1'){
                            tag="已设计";
                        }
                        tab.innerHTML+="<tr><td>"+data[i].detailsNumber+"</td><td>"+data[i].procedureName+"</td><td  class='gxbh1'>"+data[i].procedureId+"</td>" +
                            "<td>"+data[i].labourHourAmount+"</td><td>"+data[i].subtotal+"</td><td class='wlxj'>"+modelTotal+"</td>" +
                            "<td class='tag'>"+tag+"</td></tr>";
                    }
                }
            })

        },
        error:function (data) {
            alert("该产品没有设置相应物料");
        }
    })



}
let dangqiana;
function sheji(aa,that) {
    aaa=aa;
    dangqiana=that;
    $("#table11").hide();
    $(".gxwltab").hide();
    $("#cpwlmx").show();
    $("#cpwlmx1").show();
    $("#but1").show();
    $("#but2").hide();
    $("#tab22").hide();

    let id=$("#id").html();
    $.ajax({
        method: "post",
        contentType: 'application/json',
        url: "/gxwl/cpgxwlsj/"+id,
        dataType:"json",
        async:true,
        success:function (data) {
            let tab1=document.getElementById("tabsj");
            tab1.innerHTML="";
            for (let i=0;i<data.length;i++){
                let unit = data[i].amountUnit;
                if (unit==undefined){
                    unit="";
                }
                tab1.innerHTML+="<tr><td>"+data[i].detailsNumber+"</td><td>"+data[i].productName+"</td><td>"+data[i].productId+"</td>" +
                    "<td>"+data[i].productDescribe+"</td><td>"+data[i].amount+"</td><td>"+data[i].residualAmount+"</td>" +
                    "<td>"+unit+"</td><td class='dj'>"+data[i].costPrice+"</td><td><input class='sl' type=\"number\"></td></tr>"
            }
        }
    })
}

function qd1() {
    $("#table11").show();
    $("#cpwlmx1").show();

    $("#but1").hide();
    $("#but2").show();
    $("#cpwlmx").hide();
    $("#tab22").show();

    let sl=$(".sl");
    let dj=$(".dj");
    /*for (let i=0;i<sl.length;i++){
        let gj= sl[i].value*dj[i].innerHTML;
    }*/
    let id=$("#id").html();

    $.ajax({
        method: "post",
        contentType: 'application/json',
        url: "/gxwl/cpgxwlsj/"+id,
        dataType:"json",
        async:true,
        success:function (data) {
            let tab2=document.getElementById("tabtj")
            tab2.innerHTML="";
            for (let i=0;i<data.length;i++){
                let unit = data[i].amountUnit;
                if (unit==undefined){
                    unit="";
                }
                tab2.innerHTML+="<tr><td class='adxh'>"+data[i].detailsNumber+"</td><td class='adname'>"+data[i].productName+"</td><td class='adbh'>"+data[i].productId+"</td>" +
                    "<td class='adms'>"+data[i].productDescribe+"</td><td class='adsl'>"+sl[i].value+"</td><td class='addw'>"+unit+"</td>" +
                    "<td class='addj'>"+data[i].costPrice+"</td><td class='adxj'>"+sl[i].value*dj[i].innerHTML+"</td></tr>"
            }
        }
    })
}

function qd2() {
    let adxh=$(".adxh");
    let adname=$(".adname");
    let adbh=$(".adbh");
    let adms=$(".adms");
    let adsl=$(".adsl");
    let addw=$(".addw");
    let addj=$(".addj");
    let adxj=$(".adxj");

    let sum = 0;
    for (let i=0;i<adxh.length;i++){
        sum+=parseFloat(adxj[i].innerHTML);
    }

    for (let i=0;i<adxh.length;i++){
        let qq={
            id:null,
            parentId:aaa,
            detailsNumber:(i+1),
            productId:adbh[i].innerHTML,
            productName:adname[i].innerHTML,
            amount:adsl[i].innerHTML,
            productDescribe:adms[i].innerHTML,
            amountUnit:addw[i].innerHTML,
            costPrice:addj[i].innerHTML,
            subtotal:sum,
            type:adxj[i].innerHTML
        };
        $.ajax({
            method: "post",
            contentType: 'application/json',
            url: "/gxwl/addcpwlmx",
            data:JSON.stringify(qq),
            dataType:"json",
            async:true,
            success:function (data) {

            }
        })
    }

    $(".gxwltab").show();
    $("#cpwlmx1").hide();

    let id=$("#id").html();

    /*$.ajax({
        method: "post",
        contentType: 'application/json',
        url: "/gxwl/gxmxwlsj/"+iid,
        dataType:"json",
        async:true,
        success:function (data) {
            let tab=document.getElementById("wlsjmx");
            tab.innerHTML="";
            for (let i=0;i<data.length;i++){
                let sum = data[i].moduleSubtotal;
                if (sum==undefined){
                    sum=0;
                }
                // aaa.push(data[i].procedureId)
                tab.innerHTML+="<tr><td>"+data[i].detailsNumber+"</td><td>"+data[i].procedureName+"</td><td>"+data[i].procedureId+"</td>" +
                    "<td>"+data[i].labourHourAmount+"</td><td>"+data[i].subtotal+"</td><td>"+sum+"</td>" +
                    "<td><a href='#' onclick='sheji("+data[i].id+",this)'>设计</a></td></tr>"
            }
        }
    })*/
    $(dangqiana).parent().prev().html(sum);
    $(dangqiana).parent().html("已设计");
}


function qdtj() {
    let mx=$(".wlxj")
    let z=0;
    let arr=[];

    for (let i=0;i<mx.length;i++){
        z+=parseFloat(mx[i].innerHTML)
        let dxx={id:aar[i],moduleSubtotal:mx[i].innerHTML}
        arr[i]=dxx
    }
    let ddd={id:iid,moduleCostPriceSum:z,list:arr};
    var tag = $(".tag:last").html();
    if(tag=="已设计"){
    if (confirm("确定提交设计单吗？")){
        $.ajax({
            type:"put",
            contentType: 'application/json',
            url: "/gxwl/putgxwl",
            data:JSON.stringify(ddd),
            dataType:"json",
            traditional:true,
            async:true,
            success:function (data) {
                parent.document.getElementById('iframeId').contentWindow.location.reload(true);
            }
        })
    }
    }else {
        alert("请先完成全部的物料设计");
    }

}